package com.swust.ipmsg.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.IBinder;

import com.swust.ipmsg.receiver.ConnectionReceiver;
import com.swust.ipmsg.util.IPMsg;
import com.swust.ipmsg.util.NetWork;
import com.swust.ipmsg.util.Packet;
import com.swust.ipmsg.util.Person;

@SuppressLint("ShowToast")
public class IPmsgService extends Service {

	ConnectionReceiver connectionReceiver = null;
	private Person me;
	public final String BROADCAST_ADDRESS = "255.255.255.255";
	private Packet packet;

	Thread onLineThread = null;
	Thread onLineThreadSustaine = null;
	Thread offLineThread = null;
	Thread onLineListenerThread = null;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		System.out.println("onCreate!");

		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		connectionReceiver = new ConnectionReceiver();
		registerReceiver(connectionReceiver, intentFilter);
		
		
	}
	
	
	
	class OnLineThread extends Thread {
		@Override
		public void run() {
			PacketInternet.broadcastMessage(packet);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("I'm onLine");
		}

	}
	
	class OnLineThreadSustaine extends Thread {
		@Override
		public void run() {
			int i = 0;
			while (i < 4) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				PacketInternet.broadcastMessage(packet);
				System.out.println("I'm onLine");
				i++;
			}
			super.run();
		}

	}
	
	
	class OffLineThread extends Thread {
		@Override
		public void run() {
			packet.setCommandNO(IPMsg.IPMSG_BR_EXIT);
			packet.setTerminalVersion("");
			PacketInternet.sendMessage(packet, BROADCAST_ADDRESS);
			System.out.println("I'm onLine");
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			super.run();
		}

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		
		me = (Person) intent.getSerializableExtra("me");
		long packet_number = Calendar.getInstance().getTime().getTime();
		String msgString = IPMsg.IPMSG_VERSION + ":" + packet_number + ":"
				+ me.getUseName() + ":" + IPMsg.IPMSG_TERMINAL_VERSION + ":" + me.getUseName() + ":"
				+ IPMsg.IPMSG_NEW_BR_ENTRY + ":" + me.getUseName();
		packet = new Packet(msgString);
		if (NetWork.isWifiConnected(this)) {
			System.out.println("服务中WIFI连接成功！");
			onLineThread = new OnLineThread();
			onLineThread.start();
			onLineThreadSustaine = new OnLineThreadSustaine();
			onLineThreadSustaine.start();
		} else {
			System.out.println("服务中WIFI连接失败！");
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		offLineThread = new OffLineThread();
		offLineThread.start();
		/*try {
            Thread.sleep(2000);
        } catch ( InterruptedException x ) {
            // ignore
        }
		onLineThreadSustaine.stop();
		
		try {
            Thread.sleep(2000);
        } catch ( InterruptedException x ) {
            // ignore
        }
		onLineThread.stop();
		*/
		if (connectionReceiver != null) {
			unregisterReceiver(connectionReceiver);
		}
		System.out.println("onDestroy中可以释放一些资源!");
		super.onDestroy();
	}



	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
/*
	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		super.onRebind(intent);
		System.out.println("onRebind");
	}*/

}